library(corrplot)
df <- read.csv("data_with_media_measures.csv")
head(df)

cnames <- c("pre_partisan_news_flow", "pre_top_media_slant",
            "pre_outlet_diversity", "pre_type_diversity",
            "pre_far_right", "pre_far_left", 
            "partisan_news_flow","top_media_slant",
            "outlet_diversity","type_diversity",
            "far_right","far_left", "partisanship2")
df_cor <- df[,cnames]
head(df_cor)
df_cor$change_partisan_news_flow <- df_cor$partisan_news_flow -
  df_cor$pre_partisan_news_flow
df_cor$change_top_media_slant <- df_cor$top_media_slant -
  df_cor$pre_top_media_slant
df_cor$change_outlet_diversity <- df_cor$outlet_diversity -
  df_cor$pre_outlet_diversity
df_cor$change_type_diversity <- df_cor$type_diversity -
  df_cor$pre_type_diversity
df_cor$change_far_right <- df_cor$far_right -
  df_cor$pre_far_right
df_cor$change_far_left <- df_cor$far_left -
  df_cor$pre_far_left


df_cor$res_partisan_news_flow <- residuals(lm(partisan_news_flow ~ pre_partisan_news_flow, data = df_cor, na.action = na.exclude))
df_cor$res_top_media_slant <- residuals(lm(top_media_slant ~ pre_top_media_slant, data = df_cor, na.action = na.exclude))
df_cor$res_outlet_diversity <- residuals(lm(outlet_diversity ~ pre_outlet_diversity, data = df_cor, na.action = na.exclude))
df_cor$res_type_diversity <- residuals(lm(type_diversity ~ pre_type_diversity, data = df_cor, na.action = na.exclude))
df_cor$res_far_right <- residuals(lm(far_right ~ pre_far_right, data = df_cor, na.action = na.exclude))
df_cor$res_far_left <- residuals(lm(far_left ~ pre_far_left, data = df_cor, na.action = na.exclude))



df_cor
res <- cor(df_cor, use = "pairwise.complete.obs")
res

df_res <- as.data.frame(res)
write.csv(df_res, "correlation table-test-full.csv")

options(scipen=999)

cor.test(df$partisan_news_flow, df$top_media_slant)
cor.test(df$outlet_diversity, df$type_diversity)

